﻿using System;

namespace SharpTag.ID3
{
    /// <summary>
    /// Concrete implementation of the <see cref="AbstractTagFactory"/> class.
    /// </summary>
    public class ID3TagFactory : AbstractTagFactory
    {
        private const int TAGIDLENGTH = 3;
        private const int ID3V1TAGLENGTH = 128;
        private const int ID3V2TAGOFFSET = 0;

        /// <summary>
        /// Creates a new <see cref="ITag"/> object.
        /// </summary>
        /// <param name="tagBytes">Raw byte array representing the Tag info.</param>
        /// <param name="fileLength">Length of the file that contains the tag.</param>
        /// <returns>A new <see cref="ITag"/> object.</returns>
        public override ITag CreateTag(byte[] tagBytes, long fileLength)
        {
            char[] idBytes = new char[TAGIDLENGTH];
            Array.Copy(tagBytes, 0, idBytes, 0, TAGIDLENGTH);
            var idString = new string(idBytes);
            ITag returnTag = null;

            switch (idString)
            {
                case "ID3":
                    returnTag = new ID3V2Tag(ID3V2TAGOFFSET);
                    break;
                case "TAG":
                    returnTag = new ID3V1Tag(fileLength - ID3V1TAGLENGTH);
                    break;
            }

            if(returnTag != null)
                returnTag.ParseTagData(tagBytes);

            return returnTag;
        }
    }
}
